package com.tangsm.spring.boot.validation.controller;

import com.tangsm.spring.boot.validation.domain.entity.User;
import com.tangsm.spring.boot.validation.domain.vo.Result;
import com.tangsm.spring.boot.validation.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.Min;

/**
 * 用户管理
 *
 * @author tangsm
 */
@RestController
@RequestMapping("/v1/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 根据ID查询
     *
     * @return 响应信息
     */
    @GetMapping
    public Result<User> get(@RequestParam @Min(100) Integer id) {
        return Result.success(userService.get(id));
    }

    /**
     * 添加
     *
     * @param user 参数实体
     * @return 响应信息
     */
    @PostMapping
    public Result<User> add(@RequestBody User user) {
        // 添加用户
        if (userService.add(user)) {
            return Result.success();
        }

        return Result.failure();
    }
}
